package com.zxk.version1.provider;



import com.zxk.version1.core.User;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author 朱小康
 */
public class RpcServer {

    public static void main(String[] args) {
        //服务端以BIO的方式监听socket

        UserServiceImpl userService = new UserServiceImpl();

        try {
            ServerSocket serverSocket = new ServerSocket(8081);
            System.out.println("服务端启动了");
            while (true){
                Socket socket = serverSocket.accept();
                new Thread(()->{
                    try {
                        InputStream inputStream = socket.getInputStream();
                        OutputStream outputStream = socket.getOutputStream();
                        ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
                        ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);

                        //读取客户端传过来的id
                        Integer id = objectInputStream.readInt();
                        //查询user信息
                        User user = userService.getUserByUserId(id);
                        objectOutputStream.writeObject(user);

                        objectOutputStream.flush();

                    } catch (IOException e) {
                        e.printStackTrace();
                    }


                }).start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
